package com.drg.center.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.drg.center.entity.param.SysDictAddDTO;
import com.drg.center.entity.param.SysDictEditDTO;
import com.drg.center.entity.param.SysDictListDTO;
import com.drg.center.entity.result.SysDictItemVO;
import com.drg.center.entity.result.SysDictVO;
import com.guaike.core.entity.enums.EnableStatusEnum;
import com.guaike.core.entity.param.EditStatusDTO;
import com.guaike.core.entity.result.PageEntity;
import com.guaike.core.entity.result.Result;
import com.guaike.core.utils.ListUtil;
import com.drg.center.dao.SysDictDAO;
import com.drg.center.entity.SysDictPO;
import com.drg.center.service.DictItemService;
import com.drg.center.service.DictService;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: zhangxilin
 * @Date: 2023/10/26
 */
@Slf4j
@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private SysDictDAO sysDictDAO;
    @Autowired
    private DictItemService dictItemService;

    @Override
    public Result<PageEntity<SysDictVO>> list(SysDictListDTO param) {
        List<SysDictVO> list = Lists.newArrayList();
        IPage<SysDictPO> iPage =  sysDictDAO.findPage(param);
        if(CollectionUtils.isEmpty(iPage.getRecords())){
            return Result.success().data(new PageEntity<>(0L,list)).build();
        }
        List<String> dictCodeList = iPage.getRecords().stream().map(SysDictPO::getDictCode).collect(Collectors.toList());
        Map<String, List<SysDictItemVO>> itemMap = dictItemService.findMap(dictCodeList);

        for(SysDictPO po: iPage.getRecords()){
            SysDictVO dictVO = new SysDictVO();
            BeanUtils.copyProperties(po, dictVO);
            dictVO.setItems(itemMap.get(po.getDictCode()));
            list.add(dictVO);
        }
        return Result.success().data(new PageEntity<>(iPage.getTotal(),list)).build();
    }

    @Override
    public Result<Boolean> add(SysDictAddDTO param) {
        if(sysDictDAO.isExistCode(param.getDictCode())){
            return Result.failure().message("字典编码已存在").build();
        }
        SysDictPO record = new SysDictPO();
        record.setDictCode(param.getDictCode());
        record.setDictName(param.getDictName());
        record.setRemark(param.getRemark());
        record.setStatus(EnableStatusEnum.ENABLE.getCode());
        record.setIsSys(param.getIsSys()==null? Boolean.FALSE: param.getIsSys());
        sysDictDAO.save(record);
        return Result.success().data(Boolean.TRUE).build();
    }

    @Override
    public Result<Boolean> edit(SysDictEditDTO param) {
        SysDictPO record = sysDictDAO.findById(param.getId());
        if(record ==null){
            return Result.failure().message("记录不存在").build();
        }
        record.setDictName(param.getDictName());
        record.setRemark(param.getRemark());
        record.setIsSys(param.getIsSys()==null? Boolean.FALSE: param.getIsSys());
        sysDictDAO.updateById(record);
        return Result.success().data(Boolean.TRUE).build();
    }


    @Override
    public Result<Boolean> delete(Long id) {
        SysDictPO record = sysDictDAO.findById(id);
        if(record ==null){
            return Result.failure().message("记录不存在").build();
        }
        if(record.getIsSys() !=null && record.getIsSys()){
            return Result.failure().message("系统字典禁止删除").build();
        }
        sysDictDAO.delete(record);
        dictItemService.syncCache(record.getDictCode());
        return Result.success().data(Boolean.TRUE).build();
    }
}
